﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using MillerApp.CommonLib.Utility;
using MillerApp.UserControls;
using MillerApp.DataContract.MillerDB;
using MillerApp.DataAccess.MillerDB;

namespace MillerApp.Stock.Common
{
    public class Common
    {
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="password"></param>
        /// <param name="errorMsg"></param>
        /// <param name="currentUser"></param>
        /// <returns></returns>
        public static bool Login(string userid,string password,out string errorMsg,out UserEntity currentUser)
        {
            errorMsg = string.Empty;
            currentUser = null;
            currentUser = UserManager.GetUserEntity(userid);
            if (currentUser == null)
            {
                errorMsg = "用户名错误";
                return false;
            }
            string usrpwd = currentUser.password;
            if (usrpwd != password)
            {
                errorMsg = "密码错误";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取当前用户
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public static UserEntity GetCurrentUser(Form form)
        {
            if (form == null)
            {
                return null;
            }
            else if (form is MainForm)
            {
                MainForm f=form as MainForm;
                return f.CurrentUser;
            }
            else if (form.MdiParent is MainForm)
            {
                MainForm f = form.MdiParent as MainForm;
                return f.CurrentUser;
            }
            return null;
        }
    }
}
